 /*
 * MainGUI.java
 *
 * Created on 06 November 2008, 18:34
 */
package bluetoothserver.graphical;

import bluetoothserver.BluetoothInterface;
import bluetoothserver.dataStore.*;
import java.awt.Image;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
 
/**
 *
 * @author  Philip
 */
public class MainGUI extends javax.swing.JFrame {
    private static MainGUI instance;
    private QuestionCollection questions;
    private DefaultListModel qs;
    private Thread blueInter;
    BluetoothInterface blue;
    
    /** Creates new form MainGUI */
    private MainGUI() {
        blueInter = new BluetoothInterface();
        qs = new DefaultListModel();
        initComponents();
        setLF();
        pack();
        this.setTitle("BlueQ Control Server");
        this.setSize(this.getWidth()-50, this.getHeight());
        this.setLocation((HEIGHT/2)+200, (WIDTH/2)+150);
        questions = new QuestionCollection();
        this.updateQuestionList();
        this.setVisible(true);
        this.setIconImage(getIcon());

    }
    
    public static MainGUI getInstance(){
        if(instance == null){
            instance = new MainGUI();
        }
        return instance;
    }

    private Image getIcon(){
        Image i = null;
        try{
        File f = new File("Logo-small.png");
        i = javax.imageio.ImageIO.read(f);
        }catch(Exception e){}
        return i;
    }

    private void changeSelectable(boolean a, boolean b, boolean c, boolean d, boolean e, boolean f) {
        jCheckBox1.setEnabled(a);
        jCheckBox2.setEnabled(b);
        jCheckBox3.setEnabled(c);
        jCheckBox4.setEnabled(d);
        jCheckBox5.setEnabled(e);
        jCheckBox6.setEnabled(f);
    }

    private void changeState(boolean state) {
        jTextField2.setEditable(state);
        jTextField3.setEditable(state);
        jTextField4.setEditable(state);
        jTextField5.setEditable(state);
        jTextField6.setEditable(state);
        jTextField1.setEditable(state);
    }

    private void changeText(String a, String b, String c, String d, String e, String f) {
        jTextField1.setText(a);
        jTextField2.setText(b);
        jTextField3.setText(c);
        jTextField4.setText(d);
        jTextField5.setText(e);
        jTextField6.setText(f);
        
    }
 
    private void updateQuestionList(){
        ArrayList<Question> q = questions.getList();
        Iterator<Question> i = q.iterator();
        qs.clear();
        while(i.hasNext()){
            qs.addElement(i.next().getQuestion());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //IGNORED
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        questionTitle = new javax.swing.JTextField();
        addQ = new javax.swing.JButton();
        removeQ = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        updateQ = new javax.swing.JButton();
        beginQ = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jTextField5 = new javax.swing.JTextField();
        jTextField6 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        jCheckBox5 = new javax.swing.JCheckBox();
        jCheckBox6 = new javax.swing.JCheckBox();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        exportq = new javax.swing.JMenuItem();
        importq = new javax.swing.JMenuItem();
        exit = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        addq = new javax.swing.JMenuItem();
        nextq = new javax.swing.JMenuItem();
        previousq = new javax.swing.JMenuItem();
        removeq = new javax.swing.JMenuItem();
        veiwComments = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        jLabel1.setText("Question Type");

        jLabel2.setText("Question Title");

        addQ.setText("Add Question");
        addQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                addQMousePressed(evt);
            }
        });

        removeQ.setText("Remove Question");
        removeQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                removeQMousePressed(evt);
            }
        });

        jList1.setModel(qs);
        jList1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jList1MousePressed(evt);
            }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        updateQ.setText("Update Question");
        updateQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                updateQMousePressed(evt);
            }
        });

        beginQ.setText("Begin Quiz");
        beginQ.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                beginQMousePressed(evt);
            }
        });

        jCheckBox1.setText("1)");

        jCheckBox2.setText("2)");

        jCheckBox3.setText("3)");

        jCheckBox4.setText("4)");

        jCheckBox5.setText("5)");

        jCheckBox6.setText("6)");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Multichoice", "Numeric", "Yes / No", "True / False", "Sequencing", "Multimark", "Voting", "Rating Scale", "Text Entry" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jButton1.setText("Start bluetooth");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });

        jMenu1.setText("File");

        exportq.setText("Export Questions");
        exportq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                exportqMousePressed(evt);
            }
        });
        jMenu1.add(exportq);

        importq.setText("Import Questions");
        importq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                importqMousePressed(evt);
            }
        });
        jMenu1.add(importq);

        exit.setText("Exit");
        exit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                exitMousePressed(evt);
            }
        });
        jMenu1.add(exit);

        jMenuBar1.add(jMenu1);

        jMenu4.setText("Questions");

        addq.setText("Add Question");
        addq.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addqActionPerformed(evt);
            }
        });
        jMenu4.add(addq);

        nextq.setText("Next Question");
        nextq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                nextqMousePressed(evt);
            }
        });
        jMenu4.add(nextq);

        previousq.setText("Previous Question");
        previousq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                previousqMousePressed(evt);
            }
        });
        jMenu4.add(previousq);

        removeq.setText("Remove Question");
        removeq.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                removeqMousePressed(evt);
            }
        });
        jMenu4.add(removeq);

        jMenuBar1.add(jMenu4);

        veiwComments.setText("Comments");
        veiwComments.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                veiwCommentsMousePressed(evt);
            }
        });

        jMenuItem2.setText("View Comments");
        jMenuItem2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem2MousePressed(evt);
            }
        });
        veiwComments.add(jMenuItem2);

        jMenuBar1.add(veiwComments);

        jMenu3.setText("Help");

        jMenuItem1.setText("About");
        jMenuItem1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuItem1MousePressed(evt);
            }
        });
        jMenu3.add(jMenuItem1);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jCheckBox6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 577, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(addQ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(updateQ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeQ)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(beginQ))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(questionTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 614, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(questionTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox2)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox3)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox4)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox5)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox6)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(beginQ)
                        .addComponent(jButton1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(updateQ)
                        .addComponent(addQ))
                    .addComponent(removeQ))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void addqActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addqActionPerformed
    this.addQuestion();
}//GEN-LAST:event_addqActionPerformed
    
private void importqMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_importqMousePressed
    JFileChooser chooser = new JFileChooser();
    FileFilter fil = new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".bq");
        }
        @Override
        public String getDescription() {
            return "BlueQ Question Files";
        }
    };
    chooser.setFileFilter(fil);
    int returnVal = chooser.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        File f = chooser.getSelectedFile();
        SavedFileImporter collector = new SavedFileImporter(f.getAbsolutePath());
        ArrayList<Question> im = collector.importQuestions();
        if(im != null){
            questions.importArrray(im);
            this.updateQuestionList();
        }
    }
}//GEN-LAST:event_importqMousePressed

private void exitMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exitMousePressed
    System.exit(0);
}//GEN-LAST:event_exitMousePressed

private void exportqMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_exportqMousePressed
    JFileChooser chooser = new JFileChooser();
    FileFilter fil = new FileFilter() {
        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().toLowerCase().endsWith(".bq");
        }
        @Override
        public String getDescription() {
            return "BlueQ Question Files";
        }
    };
    chooser.setFileFilter(fil);
    int returnVal = chooser.showSaveDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
        File f = chooser.getSelectedFile();
        SavedFileExporter writer = new SavedFileExporter();
        writer.saveQuestion(f, questions);
    }
}//GEN-LAST:event_exportqMousePressed

private void beginQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_beginQMousePressed
    if(!questions.getList().isEmpty()){
        this.setVisible(false);
        IQMenu menu = new IQMenu(questions.getList(), this,blue);
        menu.setVisible(true);
    }
}//GEN-LAST:event_beginQMousePressed

private void updateQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_updateQMousePressed
    int i = jList1.getSelectedIndex();
    Question q = collectData();
    questions.replaceQuestion(i, q);
    this.updateQuestionList();
    jList1.setSelectedIndex(i);  
}//GEN-LAST:event_updateQMousePressed

private void addQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_addQMousePressed
    this.addQuestion();
}//GEN-LAST:event_addQMousePressed

private void removeQMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removeQMousePressed
    this.removeQuestion();
}//GEN-LAST:event_removeQMousePressed

private void removeqMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_removeqMousePressed
    this.removeQuestion();
}//GEN-LAST:event_removeqMousePressed

private void nextqMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nextqMousePressed
    int i = jList1.getSelectedIndex();
    jList1.setSelectedIndex(i+1);
}//GEN-LAST:event_nextqMousePressed

private void previousqMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_previousqMousePressed
    int i = jList1.getSelectedIndex();
    jList1.setSelectedIndex(i-1);
}//GEN-LAST:event_previousqMousePressed

private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
    String s = (String)jList1.getSelectedValue();
    if(s != null){
        Question selected = questions.getQuestion(s);
        this.updateQuestion(selected);
    }
}//GEN-LAST:event_jList1ValueChanged

private void jList1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MousePressed

}//GEN-LAST:event_jList1MousePressed

private void veiwCommentsMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_veiwCommentsMousePressed

}//GEN-LAST:event_veiwCommentsMousePressed

private void jMenuItem2MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem2MousePressed
    CommentDialog com = new CommentDialog(this,true);
    com.setVisible(true);
}//GEN-LAST:event_jMenuItem2MousePressed

private void jMenuItem1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem1MousePressed
    About about = new About(this.getX(),this.getY());
    about.setVisible(true);
}//GEN-LAST:event_jMenuItem1MousePressed

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
    int i = jComboBox1.getSelectedIndex();
    switch(i){
        case QuestionCollection.YES_NO:
            this.changeState(false);
            this.changeText("Yes","No","","","","");
            this.changeSelectable(true,true,false,false,false,false);
            break;
        case QuestionCollection.TRUE_FALSE:
            this.changeState(false);
            this.changeText("True","False","","","","");
            this.changeSelectable(true,true,false,false,false,false);
            break;
        case QuestionCollection.SEQUENCE:
            this.changeState(true);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
            break;
        case QuestionCollection.VOTING:
            this.changeState(true);
            this.changeText("","","","","","");
            this.changeSelectable(false,false,false,false,false,false);
        case QuestionCollection.RATING:
            this.changeState(false);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false); 
            break;
        case QuestionCollection.TEXT_ENTRY:
            this.changeState(false);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(false,false,false,false,false,false);
            break;
        default:
            this.changeState(true);
            this.changeText("", "", "", "", "", "");
            this.changeSelectable(true,true,true,true,true,true);
            break;
    }
}//GEN-LAST:event_jComboBox1ItemStateChanged

private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
    //Starts the bluetooth service and sets the first question to be sent 
    if(!questions.getList().isEmpty()){
        blueInter.start();
        blue = (BluetoothInterface)blueInter;
        blue.SendQuestion(questions.getList().get(0));
        jButton1.setEnabled(false);
    }
    //Locks the questions so they can't be edited
}//GEN-LAST:event_jButton1MousePressed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addQ;
    private javax.swing.JMenuItem addq;
    private javax.swing.JButton beginQ;
    private javax.swing.JMenuItem exit;
    private javax.swing.JMenuItem exportq;
    private javax.swing.JMenuItem importq;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JCheckBox jCheckBox5;
    private javax.swing.JCheckBox jCheckBox6;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JMenuItem nextq;
    private javax.swing.JMenuItem previousq;
    private javax.swing.JTextField questionTitle;
    private javax.swing.JButton removeQ;
    private javax.swing.JMenuItem removeq;
    private javax.swing.JButton updateQ;
    private javax.swing.JMenu veiwComments;
    // End of variables declaration//GEN-END:variables

    private void updateQuestion(Question q){
        clearAnswers();
        questionTitle.setText(q.getQuestion());
        jComboBox1.setSelectedIndex(q.getType());
        String[] an = q.getAnswers();
        this.setAnswers(an);
        this.setCorrect(q.getCorrectAnswer(),q.getType());
    }
    
    private Question collectData(){
        Question q = new Question();
        q.setQuestion(questionTitle.getText());
        q.setType(jComboBox1.getSelectedIndex());
        String[] an = new String[6];
        an[0] = jTextField1.getText();
        an[1] = jTextField2.getText();
        an[2] = jTextField3.getText();
        an[3] = jTextField4.getText();
        an[4] = jTextField5.getText();
        an[5] = jTextField6.getText();
        q.setAnswers(an);
        int i;
        if(q.getType() == QuestionCollection.MULTIMARK){
            i = getCorrect();
        }else{
            i = multiCorrect();
        }
        q.setCorrectAnswer(i);
        return q;
    }
    
    /**
     * This method collects the correct answer from the dialog
     * @return int value that is the answer
     */
    private int getCorrect(){
        int i = 0;
        if(jCheckBox1.isSelected()){
            i = 1;
        }else if(jCheckBox2.isSelected()){
            i = 2;
        }else if(jCheckBox3.isSelected()){
            i = 3;
        }else if(jCheckBox4.isSelected()){
            i = 4;
        }else if(jCheckBox5.isSelected()){
            i = 5;
        }else if(jCheckBox6.isSelected()){
            i = 6;
        }
        return i;
    }

    private int multiCorrect(){
        String s = "0";
        if(jCheckBox1.isSelected()){
            s = s+1;
        }
        if(jCheckBox2.isSelected()){
            s = s+2;
        }
        if(jCheckBox3.isSelected()){
            s = s+3;
        }
        if(jCheckBox4.isSelected()){
            s = s+4;
        }
        if(jCheckBox5.isSelected()){
            s = s+5;
        }
        if(jCheckBox6.isSelected()){
            s = s+6;
        }
        return Integer.parseInt(s);
    }

    private void setAnswers(String[]a){
            if((a[0] == null)||(a[0].compareTo("null")==0)){
                jTextField1.setText("");
            }else{
                jTextField1.setText(a[0]);
            }
            if((a[1] == null)||(a[1].compareTo("null")==0)){
                jTextField2.setText("");
            }else{
                jTextField2.setText(a[1]);
            }
            if((a[2] == null)||(a[2].compareTo("null")==0)){
                jTextField3.setText("");
            }else{
                jTextField3.setText(a[2]);
            }
            if((a[3] == null)||(a[3].compareTo("null")==0)){
                jTextField4.setText("");
            }else{
                jTextField4.setText(a[3]);
            }
            if((a[4] == null)||(a[4].compareTo("null")==0)){
                jTextField5.setText("");
            }else{
                jTextField5.setText(a[4]);
            }
            if((a[5] == null)||(a[5].compareTo("null")==0)){
                jTextField6.setText("");
            }else{
                jTextField6.setText(a[5]);
            }
    }

    private void addQuestion(){
        QuestionAddDialog qDialog = new QuestionAddDialog(this,true);
        if(qDialog.isCompleted()){
            int i = jList1.getSelectedIndex()+1;
            Question q = qDialog.getQuestion();
            questions.addQuestion(q);
            this.updateQuestionList();
            jList1.setSelectedIndex(i);
        }
    }
    
    private void removeQuestion(){
    clearAnswers();
    questionTitle.setText("");
    int i = jList1.getSelectedIndex();
    questions.remove(i);
    this.updateQuestionList();
    jList1.setSelectedIndex(i);        
    }
    
    private void setLF(){
        UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
        try{
            UIManager.setLookAndFeel(plaf[3].getClassName());
            SwingUtilities.updateComponentTreeUI(this);
        }catch(Exception e){}
    }

    private void clearAnswers(){
        jTextField1.setText("");
        jTextField2.setText("");
        jTextField3.setText("");
        jTextField4.setText("");
        jTextField5.setText("");
        jTextField6.setText("");
    }

    private void setCorrect(int i,int t){
        this.clearCorrect();
        //This decides if the question is a multimark or not
        if(t == 5){
            //Multimark answers
            String s = i+"";
            
            for(int j = 0;j < s.length();j++){
                int k = (int)s.charAt(j);
                
                this.setSelectedBox(k-48);
            }
        }else{
            //standard single answer
            setSelectedBox(i);
        }
    }

    private void setSelectedBox(int i){
        switch(i){
            case 1:
                jCheckBox1.setSelected(true);
                break;
            case 2:
                jCheckBox2.setSelected(true);
                break;
            case 3:
                jCheckBox3.setSelected(true);
                break;
            case 4:
                jCheckBox4.setSelected(true);
                break;
            case 5:
                jCheckBox5.setSelected(true);
                break;
            case 6:
                jCheckBox6.setSelected(true);
                break;
        }
    }

    private void clearCorrect(){
        jCheckBox1.setSelected(false);
        jCheckBox2.setSelected(false);
        jCheckBox3.setSelected(false);
        jCheckBox4.setSelected(false);
        jCheckBox5.setSelected(false);
        jCheckBox6.setSelected(false);
    }
}